package com.example.concurrency.immutable;

import com.example.concurrency.annotation.NotThreadSafe;
import com.example.concurrency.annotation.ThreadSafe;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;

/**
 * 不可变对象
 *
 * @author 文希
 * @create 2019-05-19 14:11
 */
@Slf4j
@ThreadSafe
public class ImmutableExample2 {

    // 此处不在需要final修饰了
    private static Map<Integer,Integer> map = Maps.newHashMap();

    static {
        map.put(1, 2);
        map.put(3, 4);
        map.put(5, 6);
        map = Collections.unmodifiableMap(map); // 另外一种定义不可变对象的方式
    }

    public static void main(String[] args) {
         // 报错
        map.put(1,3);
        log.info("{}",map.get(1)); // 3
    }
}
